<?php
/**
 * Created by PhpStorm.
 * User: Aliaksei Lazerka
 * Date: 10/21/14
 * Time: 8:28 PM
 */

include_once("functions/db_manipulate.php");

global $output;
$output = "";

switch ($_GET['act']) {
  case "ctg":
    generateCategoriesList();
    break;
  case "lsr":
    generateListOfRootElems();
    break;
  case "lss":
    generateListOfSubElems();
    break;
  case "gfl":
    generateFieldsList();
    break;
}


function generateCategoriesList()
{
  global $output;

  $result = getMainCategories();

  $output = "<select id='sectionId'>";

  $output .= "<option style='text-align: center'>-- Выберите вариант из списка --</option>";

  while ($row = mysql_fetch_assoc($result)) {
    $output .= "<option value='" . $row['ID'] . "'>" . $row['TITLE'] . "</option>";
  }

  $output .= "</select>";
}

function generateListOfRootElems()
{

  global $output;

  $result = getListRoots($_POST['sectionId']);
//  $result = getListRoots(1);

  $output = "<select id='listRoot' name='listRoot'>";

  $output .= "<option style='text-align: center'>-- Выберите вариант из списка --</option>";

  while ($row = mysql_fetch_assoc($result)) {
    $output .= "<option value='" . $row['ID'] . "'>" . $row['TITLE'] . "</option>";
  }

  $output .= "</select>";
}

function generateListOfSubElems()
{

  global $output;

  $result = getListSubElems($_POST['listRoot']);
//  $result = getListSubElems(5);

  $output = "<select id='listSub' name='listSub'>";

//  $output .= "<option style='text-align: center'>-- Выберите вариант из списка --</option>";

  while ($row = mysql_fetch_assoc($result)) {
    $title = $row['TITLE'] == "" ? " пусто" : $row['TITLE'];
    $output .= "<option value='" . $row['ID'] . "'>" . $title . "</option>";
  }

  $output .= "</select>";
}

function generateFieldsList()
{
  global $output;

  $aliases = getTableAliases($_POST['sectionId']);

  foreach ($aliases as $fieldName => $fieldAlias) {
    $output .= "<div id='" . $fieldName . "' class='tmp' onclick='selectDiv(this.id)'>" . $fieldAlias . "</div>";
  }
}

echo $output;